package com.qen.ktpython

import cn.hutool.poi.excel.ExcelUtil
import it.skrape.core.htmlDocument
import it.skrape.fetcher.HttpFetcher
import it.skrape.fetcher.basic
import it.skrape.fetcher.response
import it.skrape.fetcher.skrape

fun main() {
    val quoteList: MutableList<Quote> = ArrayList()
    skrape(HttpFetcher) {
        // 连接导目标页面
        request {
            url = "https://quotes.toscrape.com"
            headers = mutableMapOf(

            )
            userAgent =
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.34"
            proxy = proxyBuilder {
//                type = Proxy.Type.HTTP
//                host = "brd.superproxy.io"
//                port = 7890
            }
            authentication = basic {
//                username = "brd-customer-hl_4hgu8dwd-zone-residential"
//                password = "ZZZZZZZZZ"
            }

        }
        // skrape{it}将使用前面提到的HttpFetcher类向给定的URL发起同步HTTP GET请求。
        response {
            htmlDocument {
//                Console.log(html)
                // 类选择器，选择class="quote"
                ".quote" {
                    findAll {
                        forEach {
                            val text = it.findFirst(".text").text
                            val author = it.findFirst(".author").text
                            val tags = it.findAll(".tag").map { tag -> tag.text }
                            val quote = Quote(
                                text = text,
                                author = author,
                                tags = tags
                            )
                            quoteList.add(quote)
                        }
                    }
                }
            }
        }
    }
    val list: MutableList<MutableList<String>> = ArrayList()
    for (qute in quoteList) {
        println("Text:${qute.text}")
        println("Author:${qute.author}")
        println("Tags:${qute.tags.joinToString(";")}")
        println()
        list.add(mutableListOf("${qute.text}", "${qute.author}", "${qute.tags.joinToString(";")}"))
    }
    val writer = ExcelUtil.getWriter("/Users/a1/Documents/Git/Kotlin/Eleven/out/quotes.xlsx")
    writer.passCurrentRow()
//    writer.merge(quoteList.size - 1, "标题")
    writer.write(quoteList, true)
    writer.close()
}

class Quote constructor(
    var text: String,
    var author: String,
    tags: List<String>?
) {
//    var text: String =
//        get() = field
//        set(value) {field = value}
//    var author: String
//        get() = field
//        set(value) {field = value}
//    var tags: List<String>?
//        get() = field
//        set(value) {field = value}


    var tags: MutableList<String> = ArrayList()

    init {
        if (tags != null) {
            this.tags.addAll(tags)
        }
    }
}